﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Utility2DLibrary
{
    public abstract class VisualObject : TexturedPrimitivePolyShape
    {
        private float mouseOver;
        [ContentSerializerIgnore]
        public float MouseOver
        {
            get { return mouseOver; }
            set { mouseOver = value; }
        }

        private Effect shaderEffect;
        [ContentSerializerIgnore]
        public Effect ShaderEffect
        {
            get { return shaderEffect; }
            set { shaderEffect = value; }
        }

        private string shaderEffectFile;
        public string ShaderEffectFile
        {
            get{return shaderEffectFile;}
            set{shaderEffectFile=value;}
        }
        private Point position;
        public Vector2 PositionVector
        {
            get { return position.ToVector2(); }
            set { position = new Point(value); }
        }
        private float orientation;
        public float Orientation
        {
            get { return orientation; }
            set { orientation = value; }
        }
        public VisualObject(Effect shader,Texture2D texture,Point InitPosition, float InitOrientation) : base(texture)
        {
            shaderEffect = shader;
            position = InitPosition;
            orientation = InitOrientation;
        }
        public void Draw(SpriteBatch spritebatch, Matrix WorldTranslationMatrix, Matrix ViewProjection)
        {
            this.DrawPrimitive(spritebatch, WorldTranslationMatrix, ViewProjection);
        }
        private void DrawPrimitive(SpriteBatch spritebatch, Matrix WorldTranslationVector, Matrix ViewProjection)
        {
            VertexPositionColorTexture[] vertexList = new VertexPositionColorTexture[PrimitivePoints.Count];
            int[] indexList = new int[PrimitivePoints.Count];


            for (int i = 0; i < PrimitivePoints.Count; i++)
            {
                indexList[i]=i;
                vertexList[i].Color=Color.White;
                Point rotatedPoint = new Point(PrimitivePoints[i]);
                rotatedPoint.Transform(centerOfMass, orientation);
                Vector2 vecXY = rotatedPoint.ToVector2();
                vertexList[i].Position = new Vector3(vecXY.X,vecXY.Y,0);
                vertexList[i].TextureCoordinate = PrimitivePoints[i].ToVector2();
            }
            int numPrimitives = 1 + (vertexList.Length - 3);

            Vector3 scaledCenter = new Vector3(centerOfMass.X * scale.X, centerOfMass.Y * scale.Y, 0);
            Matrix MyWorldMatrix = Matrix.CreateScale(scale.X, scale.Y, 0) * WorldTranslationVector * Matrix.CreateTranslation(position.X - scaledCenter.X, position.Y - scaledCenter.Y,0);

            shaderEffect.Parameters["WorldViewProjection"].SetValue(MyWorldMatrix * ViewProjection);
            shaderEffect.Parameters["texureMap"].SetValue(PolyTexture);
            shaderEffect.Parameters["selected"].SetValue(mouseOver);

            shaderEffect.Begin();
            EffectPass pass = shaderEffect.Techniques[0].Passes[0];
            pass.Begin();
            spritebatch.GraphicsDevice.VertexDeclaration = new VertexDeclaration(spritebatch.GraphicsDevice, VertexPositionColorTexture.VertexElements);
            spritebatch.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColorTexture>(PrimitiveType.TriangleStrip, vertexList, 0, indexList.Length - 1, indexList, 0, numPrimitives);
            pass.End();
            shaderEffect.End();

        }
    }
}
